package com.yaodian100.mall.channel.web;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.template.service.TemplateService;
import com.yaodian100.ec.channel.entity.ChannelTemplate;
import com.yaodian100.ec.channel.service.ChannelService;
import com.yaodian100.ec.member.entity.Member;
import com.yaodian100.ec.member.service.MemberService;

/**
 * 
 * @author apple.chang
 * 
 */
public class ReportCPAAction extends ActionSupport {

	private ChannelService channelService;
	private MemberService memberService;
	private TemplateService velocityService;
	private String date;
	private String site;

	/**
	 * 
	 */
	private static final long serialVersionUID = 8761064835879574234L;

	public String listByDay() throws IOException, ParseException {
		if (StringUtils.isBlank(date) || StringUtils.isBlank(site)) {
			return null;
		}
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		response.setContentType("text/html;charset=UTF-8");
		ChannelTemplate ct = channelService.getAvailableChannelTemplate(site, "REPORT_CPA");

		if (null != ct) {
			CommonCriteria criteria = new CommonCriteria();
			DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
			criteria.addGe("activeDate", df.parse(date + "000000"));
			criteria.addLe("activeDate", df.parse(date + "235959"));
			criteria.addEq("applySource", site);
			List<Member> members = memberService.getList(0, -1, criteria, new String[] { "activeDate asc" });
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("members", members);
			m.put("channelService", channelService);
			response.getWriter().print(velocityService.formatByStringTemplate(ct.getContents(), m));
		}
		return null;
	}

	public ChannelService getChannelService() {
		return channelService;
	}

	public void setChannelService(ChannelService channelService) {
		this.channelService = channelService;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public TemplateService getVelocityService() {
		return velocityService;
	}

	public void setVelocityService(TemplateService velocityService) {
		this.velocityService = velocityService;
	}

	public String getSite() {
		return site;
	}

	public void setSite(String site) {
		this.site = site;
	}
}
